<?php
class Application_Model_LocZones extends Exts_Db_Table_Abstract
{
    protected $_name = 'loc_zones';
    protected $_primary = 'zone_id';
	protected $_rowClass = 'Zone';
	protected $_rowsetClass = 'ZoneSet';
	
    public function getQueryList()
    {
        return $this->select();
    }

	public function findById ($id){
		return $this->fetchRow($this->select()->where("zone_id = ?", $id));
	}
    public function exists( $name, $parentId =0 )
    {
        $row = $this->fetchRow(
            $this->select()
            ->where( 'name=?', $name )
            ->where( 'parent_id', $parentId )
        );
        
        if( count( $row )) {
            return $row;
        }
        return false;
    }

    public function insertPool( $strZones )
    {
        $zones = explode( ',', $strZones );
        if( is_array( $zones ) ) {
            
            $parentId = 0;
            foreach( $zones as $z ) {
                
                $zone = $this->exists($z, $parentId );
                if(  $zone  )  {
                    $parentId = $zoneId;
                    $zoneId = $zone->zone_id;
                    $parentId = $zoneId;
                } else {
                    $zoneId = (int)$this->save( array(
                            'parent_id' => $parentId,
                            'name' => $z
                        )
                    );
                    $parentId = $zoneId;
                }
            }
            return $zoneId;
        }
        return NULL;
    }
    
	
    public function save( $bind )
    {
        $row = $this->findById($bind['zone_id']);
        if(!$row) {
            $row = $this->createRow();
            unset($bind['zone_id']);
        }
        $row->setFromArray($bind);
        
        return $row->save();
        
          
    }

    public function getStrZoneById( $zoneId )
    {
        $zone = array();
        do{
            $row = $this->fetchRow( $this->select()->where('zone_id=?', $zoneId ));
            $zone[] = $row->name;
            $zoneId = $row->parent_id;
        } while( $zoneId != 0 );
        return implode( ',', ( array_reverse ( $zone ) ) );
    }

      public function isCountrie($aZone){
            return $aZone->parent_id == 0 ;
      }
	
      public function getSubZones($aZoneID=0){
            /*
             * Los paises estan definidos con parent 0, por lo que si no se especifica zona parent devuelve las zonas.
             */
      		$consulta = " SELECT *,zoneTreeLvl(zone_id) as lvl, zoneChilds(zone_id) childs FROM loc_zones WHERE parent_id = {$aZoneID} ";
      		$subZones = $this->getAdapter()->fetchAll($consulta);
      		
            return $subZones;
      }
      
      public function getParent ($aZoneId) {
      		return $this->fetchRow($this->select()->where ("zone_id = (SELECT parent_id FROM loc_zones l WHERE zone_id = ?)", $aZoneId));
      }
      
      public function getParentLevelZones($aZoneId){
      		$consulta = " SELECT *, zoneChilds(zone_id) childs FROM loc_zones WHERE parent_id = (SELECT parent_id FROM loc_zones WHERE zone_id = {$aZoneID}) ";
      		$subZones = $this->getAdapter()->fetchAll($consulta);
      		if (count($subZones) == 0)return false;
            return $subZones;
      }
	/*
	 * Devuelve las zonas en un array asociativo ID => Nombre 
	 */
      public function getSubZonesArray($aZoneID=0){
           $zones = $this->getSubZones($aZoneID);
           $zonesArray = array();
           foreach($zones as $zone){
               $zonesArray[$zone['zone_id']] = $zone['name'];
           }
           return $zonesArray;
      }
    
     
}


class Zone extends Exts_Db_Table_Row_Abstract {
	
	public function getParent () {
		if ($this->parent_id == 0) return false;
		return $this->getTable()->findById($this->parent_id);
	}
	public function getBrothers() {
		return $this->getTable()->fetchAll($this->select()->where("parent_id = {$this->parent_id}"));
	}
	
	public function getChilds (){
		return $this->getTable()->fetchAll($this->select()->where("parent_id = {$this->zone_id}"));
	}
}
class ZoneSet extends Exts_Db_Table_Rowset_Abstract{
	public function toSelectArray () {
		if (count($this) <= 0) return array();
		$selectArray = array();
		$selectArray[-1] = "Seleccione";
		foreach($this as $zone){
			$selectArray[$zone->zone_id] = $zone->name;
		}
		return $selectArray;
	}
}






